// miniprogram/pages/newActivity/newActivity.js
const func = require('../../utils/util.js')
var openid = getApp().globalData.openid;
var db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activityTitle: '',
    infoLength: 1,
    introduceLength: 1,
    info: [{
      title: '开始时间'
    }, {
      title: '结束时间'
    }],
    introduce: [{
      title: '活动介绍'
    }, {
      title: '备注'
    }],
    img: []
  },

  deleteImg: function (e) {
    var id = Number(e.currentTarget.id);
    var img = this.data.img;
    img.splice(id, 1);
    this.setData({
      img: img
    })
  },

  addImg: function () {
    var img = this.data.img;
    if (img.length < 3) {
      wx.chooseImage({
        count: 3 - img.length,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: res => {
          for (let i = 0; i < res.tempFiles.length; i++) {
            img.push(res.tempFiles[i].path)
          }
          this.setData({
            img: img
          })
        },
      })
    } else {
      wx.showToast({
        title: '最多只能上传三张图片哟',
        icon: 'none'
      })
    }
  },

  onUpload: function (imgPath) {
    var info1 = {
      particulars: this.data.info
    }
    var info2 = {
      particulars: this.data.introduce
    }
    var title = this.data.activityTitle;
    db.collection('activityInfo').add({
      data: {
        info1: info1,
        info2: info2,
        imgPath: imgPath || [],
        joinInfo: []
      }
    }).then(res => {
      db.collection('activityList').add({
        data: {
          title: title,
          id: res._id,
          time: func.formatTime(),
          img: imgPath[0] || '',
          people: 0,
        }
      }).then(res => {
        wx.hideLoading()
        wx.navigateBack()
      }).catch(res => {
        func.hideLoading('发送')
      })
    }).catch(res => {
      func.hideLoading('发送')
    })
  },

  send: function () {
    wx.showLoading({
      title: '发送中',
    })
    if (this.data.activityTitle && this.data.info[0].content && this.data.introduce[0].content) {
      var img = this.data.img;
      var imgPath = [];
      if (img.length) {
        let i, j = 0;
        for (i = 0; i < img.length; i++) {
          let suffix = /\.\w+$/.exec(img[i])[0] //返回文件的扩展名
          wx.cloud.uploadFile({
            cloudPath: new Date().getTime() + suffix,
            filePath: img[i],
          }).then(res => {
            j++;
            imgPath.push(res.fileID)
            if (j == img.length) {
              this.onUpload(imgPath);
            }
          })
        }
      } else {
        this.onUpload(imgPath);
      }
    } else {
      wx.showToast({
        title: '内容不许为空',
        icon: "none"
      })
    }
  },

  onTitle: function (e) {
    this.data.activityTitle = e.detail.value;
  },

  onInfoTitle: function (e) {
    var id = Number(e.currentTarget.id);
    this.data.info[id].title = e.detail.value;
  },

  onInfo: function (e) {
    var id = Number(e.currentTarget.id);
    this.data.info[id].content = e.detail.value;
  },

  onIntroduceTitle: function (e) {
    var id = Number(e.currentTarget.id);
    this.data.introduce[id].title = e.detail.value;
  },

  onIntroduce: function (e) {
    var id = Number(e.currentTarget.id);
    this.data.introduce[id].content = e.detail.value;
  },

  addIntroduce: function () {
    this.data.introduce.push({
      title: '',
      content: ''
    })
    this.setData({
      introduce: this.data.introduce,
    })
  },

  addInfo: function () {
    this.data.info.push({
      title: '',
      content: ''
    })
    this.setData({
      info: this.data.info,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})